﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebSite.NewsHandle;

public partial class WebSite_m_news_edit : System.Web.UI.Page
{
    string userid,articleid, title, content,section;
    NewsHandler newsHandler;
    public WebSite_m_news_edit()
    {
        newsHandler = new NewsHandler("Data Source=Localhost;Integrated Security=SSPI;Initial Catalog=CSHDB");
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null || Session["UserIdentity"].ToString() != "2")
        {
            Response.Redirect("Default.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
        else userid = Session["UserId"].ToString();
        try
        {
            articleid = Request["articleid"].ToString();
            newsHandler = new NewsHandler("Data Source=Localhost;Integrated Security=SSPI;Initial Catalog=CSHDB");
            if (!IsPostBack)
            {
                if (articleid.Length > 0)
                {
                    DataSet ds = newsHandler.GetNewsById(int.Parse(articleid));
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        content = ds.Tables[0].Rows[0][1].ToString();
                        section = ds.Tables[0].Rows[0][4].ToString();
                        title = ds.Tables[0].Rows[0][0].ToString();
                        CatagoryDropDownList.Items.FindByValue(section).Selected = true;
                        ContentEditer.Value = content;
                        TitleTextBox.Text = title;
                    }
                }
            }  
            CancelBtn.Attributes["OnClick"] = "return confirm('你确定要退出编辑么？')";
        }
        catch(Exception ex)
        {
            //跳转到错误处理页面。
            Session["ErrorMessage"] = ex.Message;
            Session["AtPage"] = "m_news_edit.aspx";
            Response.Redirect("m_error.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
     }
    /// <summary>
    /// 提交按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void UploadBtn_Click(object sender, EventArgs e)
    {
        title = TitleTextBox.Text;
        section = CatagoryDropDownList.SelectedItem.Value;
        content = ContentEditer.Value;
        DateTime date = DateTime.Now;
        //简单的字符串替换，今后应该将这个功能完善。
        try
        {
            //content = content.Replace("\n", "<br/>");
            //content = content.Replace(" ", "&nbsp;");
            //如果是要修改文件，则更新文章
            if (articleid.Length>0)
            {
                newsHandler.ModifyNews(int.Parse(articleid), int.Parse(section), userid, title, content, date);
            }
            //如果是增添文件，则增加文章
            else
            {
                articleid = newsHandler.AddNews(int.Parse(section), userid, title, content, date);
            }
            Response.Redirect("News.aspx?id=" + articleid, false);
            return;
        }
        catch(Exception ex)
        {
            //跳转到错误处理页面。
            Session["ErrorMessage"] = ex.Message;
            Session["AtPage"] = "m_news_edit.aspx";
            Response.Redirect("m_error.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }

    }
    /// <summary>
    /// 取消按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void CancelBtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("m_news_check.aspx",false);
        return;
    }
    protected void TitleTextBox_TextChanged(object sender, EventArgs e)
    {

    }
}
